﻿var GetScreenSize = function () {
    return {
        'Width': $(window).width(),
        'Height': $(window).height()
    };
};

$(window).resize(function () {
    var _shadeBox = $('div.shadeBox');
    if (_shadeBox.length > 0) {
        _shadeBox.css({ 'height': GetScreenSize().Height + document.documentElement.scrollTop });
    }

    var _dlgBox = $('div.dlgBox');
    if (_dlgBox.length > 0) {
        var _winSize = GetScreenSize();
        var _width = _dlgBox.width();
        var _height = _dlgBox.height();
        var _pLeft = (_winSize.Width - _width) / 2;
        var _pTop = ((_winSize.Height - _height) / 2) + document.documentElement.scrollTop - 100;

        if (_pLeft < 0)
            _pLeft = 0;
        if (_pTop < 0)
            _pTop = 0;

        _dlgBox.css({ 'left': _pLeft, 'top': _pTop });
    }
});

var Alert = function (msg) {
    var _msg = msg ? msg : '提示信息！';
    var _config = arguments[1] ? arguments[1] : {};
    var _width = _config.Width ? _config.Width : 300;
    var _height = _config.Height ? _config.Height : 150;
    var _left = _config.Left ? _config.Left : 0;
    var _top = _config.Top ? _config.Top : 0;
    var _callBack = _config.CallBack ? _config.CallBack : null;

    $(document.body).css({ 'overflow': 'hidden' });

    var _iframe = $('<iframe width="100%" height="100%" src="" border="0"></iframe>');
    var _shadeBox = $('<div>').attr({ 'class': 'shadeBox' }).css({ 'height': GetScreenSize().Height + document.documentElement.scrollTop })
    .append(_iframe)
    .appendTo('body');

    var _doc = _iframe[0].contentWindow.document;
    _doc.open();
    _doc.write("<body oncontextmenu='return false;'></body>");
    _doc.close();

    var _winSize = GetScreenSize();
    var _pLeft = _left, _pTop = _top;
    if (_pLeft <= 0)
        _pLeft = (_winSize.Width - _width) / 2;
    if (_pTop <= 0)
        _pTop = ((_winSize.Height - _height) / 2) + document.documentElement.scrollTop - 100;

    var _dlgBox = $('<div>').attr({ 'class': 'dlgBox' }).css({ 'width': _width, 'height': _height, 'left': _pLeft, 'top': _pTop })
    .append(
        $('<div>').addClass('info')
        .css({ 'height': _height - 40 })
        .html('<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="100%" height="100%" align="center" valign="middle">' + _msg + '</td></tr></table>'),
        $('<div>')
        .addClass('btn')
        .append(
            $('<input>')
            .attr({ 'type': 'button', 'name': 'btn_Alert', 'value': '确定' })
            .click(function () {
                _dlgBox.hide().remove();
                _shadeBox.hide().remove();
                $(document.body).css({ 'overflow': 'auto' });
                if (_callBack)
                    _callBack();
            })
        )
    )
    .appendTo('body');
};

var Confirm = function (msg) {
    var _msg = msg ? msg : '确认消息！';
    var _config = arguments[1] ? arguments[1] : {};
    var _width = _config.Width ? _config.Width : 500;
    var _height = _config.Height ? _config.Height : 200;
    var _left = _config.Left ? _config.Left : 0;
    var _top = _config.Top ? _config.Top : 0;
    var _okTxt = _config.Txt_OK ? _config.Txt_OK : '确定';
    var _cancelTxt = _config.Txt_Cancel ? _config.Txt_Cancel : '取消';
    var _callBack_OK = _config.CallBack_OK ? _config.CallBack_OK : null;
    var _callBack_Cancel = _config.CallBack_Cancel ? _config.CallBack_Cancel : null;

    $(document.body).css({ 'overflow': 'hidden' });

    var _iframe = $('<iframe width="100%" height="100%" src="" border="0"></iframe>');
    var _shadeBox = $('<div>').attr({ 'class': 'shadeBox' }).css({ 'height': GetScreenSize().Height + document.documentElement.scrollTop })
    .append(_iframe)
    .appendTo('body');

    var _doc = _iframe[0].contentWindow.document;
    _doc.open();
    _doc.write("<body oncontextmenu='return false;'></body>");
    _doc.close();

    var _winSize = GetScreenSize();
    var _pLeft = _left, _pTop = _top;
    if (_pLeft <= 0)
        _pLeft = (_winSize.Width - _width) / 2;
    if (_pTop <= 0)
        _pTop = ((_winSize.Height - _height) / 2) + document.documentElement.scrollTop - 100;

    var _dlgBox = $('<div>').attr({ 'class': 'dlgBox' }).css({ 'width': _width, 'height': _height, 'left': _pLeft, 'top': _pTop })
    .append(
        $('<div>').addClass('info')
        .css({ 'height': _height - 40 })
        .html('<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="100%" height="100%" align="center" valign="middle">' + _msg + '</td></tr></table>'),
        $('<div>')
        .addClass('btn')
        .append(
            $('<input>')
            .attr({ 'type': 'button', 'name': 'btn_Confirm_OK', 'value': _okTxt })
            .click(function () {
                _dlgBox.hide().remove();
                _shadeBox.hide().remove();
                $(document.body).css({ 'overflow': 'auto' });
                if (_callBack_OK)
                    _callBack_OK();
            }),
            $('<input>')
            .attr({ 'type': 'button', 'name': 'btn_Confirm_Cancel', 'value': _cancelTxt })
            .css({'margin-left':'50px'})
            .click(function () {
                _dlgBox.hide().remove();
                _shadeBox.hide().remove();
                $(document.body).css({ 'overflow': 'auto' });
                if (_callBack_Cancel)
                    _callBack_Cancel();
            })
        )
    )
    .appendTo('body');
};

